<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line1 flex-s">
          <el-button type="primary" @click="onOperation({}, 'add')"
            ><el-icon style="margin-right: 2px"><Plus /></el-icon
            >新建专家/推手</el-button
          >
          <el-button @click="showUpload = true"
            ><el-icon style="margin-right: 2px"><Download /></el-icon
            >导入专家/推手</el-button
          >
          <el-button v-if="!isSuccess" type="danger" @click="downloadFailFile"
            >下载失败数据</el-button
          >
          <el-button @click="downloadTemplate">下载模板</el-button>
        </div>
        <div class="header-line2 flex-s">
          <el-input
            placeholder="请输入专家名称"
            v-model="params.zhuanjiamingcheng"
            style="width: 350px"
            size="large"
          >
            <template #append>
              <el-button
                @click="GetList"
                style="min-width: 50px"
                icon="Search"
              />
            </template>
          </el-input>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="params.yewuleixing"
            size="large"
            placeholder="类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in options"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="content productTable">
        <el-table
          ref="multipleTableRef"
          border
          :data="list"
          :lazy="true"
          :load="loading"
          style="width: calc(100% - 20px)"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            property="zjNo"
            label="编号"
            width="170"
            fixed
            align="center"
          />
          <el-table-column
            property="zhuanjiamingcheng"
            label="专家名称"
            width="130"
            align="center"
          />
          <el-table-column
            property="biaoqian"
            label="标签"
            width="130"
            align="center"
          />
          <el-table-column
            property="yewuleixing1"
            label="一级业务类型"
            width="200"
            align="center"
          />
          <el-table-column
            property="yewuleixing2"
            label="二级业务类型"
            width="200"
            align="center"
          />
          <el-table-column
            property="yewuleixing3"
            label="三级业务类型"
            width="200"
            align="center"
          />
          <el-table-column
            property="zhicheng"
            label="职称"
            width="150"
            align="center"
          />
          <el-table-column
            property="lianxifangshi"
            label="联系方式/账号"
            width="150"
            align="center"
          />
          <el-table-column
            property="mima"
            label="密码"
            width="150"
            align="center"
          />
          <el-table-column
            property="youxiang"
            label="邮箱"
            width="160"
            align="center"
          />
          <el-table-column
            property="qiye"
            label="所属公司"
            width="130"
            align="center"
          />
          <el-table-column
            property="yejishuliang"
            label="业绩案例"
            width="100"
            align="center"
          />
          <el-table-column
            property="chanpinshuliang"
            label="负责产品/数量"
            width="200"
            align="center"
          />
          <el-table-column
            property="zhuanjialaiyuan"
            label="专家来源"
            width="150"
            align="center"
          >
            <template #default="scope">
              {{ scope.row.zhuanjialaiyuan?.value }}
            </template>
          </el-table-column>
          <el-table-column label="是否认证" width="100" align="center">
            <template #default="scope">
              <span v-if="scope.row.shenhe">
                {{ scope.row.shenhe.code == 1 ? "已认证" : "未认证" }}
              </span>
            </template></el-table-column
          >
          <el-table-column
            property="createTime"
            label="创建时间"
            width="200"
            align="center"
          />
          <el-table-column label="审核状态" width="100" align="center">
            <template #default="scope">
              <span v-if="scope.row.shenhe">
                {{
                  scope.row.shenhe?.code == 0
                    ? "待审核"
                    : scope.row.shenhe?.code == 1
                    ? "审核通过"
                    : "审核未通过"
                }}
              </span>
            </template></el-table-column
          >
          <el-table-column
            fixed="right"
            label="操作"
            width="300"
            align="center"
          >
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'detail')"
                >详情</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                v-if="
                  scope.row.shenhe &&
                  scope.row.shenhe.code == 0 &&
                  check === '1'
                "
                @click.prevent="onOperation(scope.row, 'pass')"
                >通过</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                v-if="
                  scope.row.shenhe &&
                  scope.row.shenhe.code == 0 &&
                  check === '1'
                "
                @click.prevent="onOperation(scope.row, 'deny')"
                >不通过</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <h-page
          v-if="total"
          v-model:curr-page="params.current"
          :size="params.size"
          :total="total"
          @change="GetList"
        />
      </div>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="40%"
        v-model="showReview"
        center
        style="padding: 0 30px"
      >
        <el-form :model="reviewForm">
          <el-form-item label="不通过原因：">
            <el-input
              v-model="reviewForm.shenheBeizhu"
              :maxlength="150"
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-form-item class="note-button-group">
          <el-button size="large" @click="showReview = false">取消</el-button>
          <el-button class="ml50" size="large" type="primary" @click="review">
            提交
          </el-button>
        </el-form-item>
      </el-dialog>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="35%"
        v-model="showUpload"
        center
        style="padding: 0 30px"
      >
        <p
          style="
            margin-top: -30px;
            margin-bottom: 30px;
            font-weight: bold;
            font-size: 20px;
            color: #333;
          "
        >
          导入专家/推手
        </p>
        <el-form>
          <el-form-item label="上传附件：">
            <bjzOssXlsx
              v-if="showUpload"
              uploadType="expert"
              @cancel="cancel"
              @success="success"
            ></bjzOssXlsx>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { defineComponent, toRefs, reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetExpertList, ReviewExpert } from "@/api/expert";
import { GetDictionary, downloadTFile } from "@/api/system";
import { baseMsg } from "@/utils/utils";
import bjzOssXlsx from "@/components/bjz-oss-xlsx.vue";
export default defineComponent({
  components: { bjzOssXlsx },
  setup() {
    const route = useRoute();
    const router = useRouter();
    const state = reactive({
      params: {
        current: Number(route.query.page) || 1,
        size: Number(route.query.size) || 10,
        zhuanjiamingcheng: "",
        yewuleixing: "",
      },
      options: [],
      dicId: "gongyingshangpinzhuanjialeixing",
      list: [],
      total: 0,
      loading: true,
      // 审核表单
      reviewForm: {
        id: null,
        shenheBeizhu: "",
        shenhe: null,
        zjNo: "",
      },
      showReview: false,
      // 是否显示上传文件弹窗
      showUpload: false,
      // 上传的数据成功状态
      isSuccess: true,
      // 失败数据文件下载地址
      failFilePath: "",
      // 失败数据文件名
      failFileName: "",
      check: localStorage.getItem("check"),
    });
    // 查询专家类型分类
    const GetSelect = async () => {
      const res = await GetDictionary({ dicId: state.dicId });
      if (res.code == 4201) {
        state.options = res.data;
      }
    };
    const GetList = () => {
      state.loading = true;
      GetExpertList(state.params).then((res) => {
        if (res.code == 4201) {
          state.list = res.data.records;
          state.total = res.data.total;
        }
        state.loading = false;
      });
    };
    // 操作
    const onOperation = (params, operation) => {
      if (operation === "detail") {
        router.push({
          path: `/zhuanjia/detail`,
          query: {
            id: params.id,
          },
        });
      }
      if (operation === "add" || operation === "edit") {
        router.push({
          path: `/zhuanjia/info`,
          query: {
            id: params.id || null,
            type: operation,
          },
        });
      }
      if (operation === "deny" || operation === "pass") {
        state.showReview = false;
        state.reviewForm.id = params.id;
        state.reviewForm.shenheBeizhu = "";
        state.reviewForm.zjNo = params.zjNo;
        if (operation === "deny") {
          state.showReview = true;
          state.reviewForm.shenhe = -1;
        } else {
          state.reviewForm.shenhe = 1;
          ReviewExpert(state.reviewForm).then((res) => {
            if (res.code == 4202) {
              baseMsg("操作成功");
              GetList();
            }
            state.showReview = false;
          });
        }
      }
    };
    const review = () => {
      if (!state.reviewForm.shenheBeizhu) {
        baseMsg("请输入不通过原因", false);
      } else {
        ReviewExpert(state.reviewForm).then((res) => {
          if (res.code == 4202) {
            baseMsg("操作成功");
            GetList();
          }
          state.showReview = false;
        });
      }
    };
    // 下载文件方法
    const downloadFile = (url, fileName) => {
      const x = new window.XMLHttpRequest();
      x.open("GET", url, true);
      x.responseType = "blob";
      x.onload = () => {
        const url = window.URL.createObjectURL(x.response);
        const a = document.createElement("a");
        console.log(url);
        a.href = url;
        a.download = fileName;
        a.click();
      };
      x.send();
    };
    // 下载模板
    const downloadTemplate = () => {
      downloadTFile({ type: "zhuanjia" }).then((res) => {
        if (res.code == 4201) {
          downloadFile(res.data.filePath, res.data.fileName);
        }
      });
    };
    // 下载失败文件模板
    const downloadFailFile = () => {
      downloadFile(state.failFilePath, state.failFileName);
    };
    // 取消上传
    const cancel = (val) => {
      state.showUpload = val;
    };
    // 文件上传完成
    const success = (data) => {
      state.isSuccess = data.result;
      if (!data.result) {
        baseMsg("部分数据失败,请下载失败文件再重新上传", false);
        state.failFilePath = data.resultFilePath;
        state.failFileName = data.fileName;
      } else {
        baseMsg("上传成功");
      }
      GetList();
      state.showUpload = false;
    };
    watch(
      () => route.query,
      () => {
        state.params.current = Number(route.query.page) || 1;
        GetList();
      },
      { deep: true }
    );
    GetList();
    GetSelect();
    return {
      ...toRefs(state),
      downloadTemplate,
      downloadFailFile,
      cancel,
      success,
      GetList,
      onOperation,
      GetSelect,
      review,
    };
  },
});
</script>
<style lang="scss" scoped>
.productTable {
  padding-bottom: 30px;
}
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
}
</style>
<style lang="scss">
.productTable .el-table__header th {
  background-color: #f2f9fc !important;
}
.note-button-group {
  .el-form-item__content {
    justify-content: center;
  }
}
</style>
